package cn.hg.jfsa.web.theme;

import cn.hg.base.jfinal.directive.BaseDirective;
import cn.hg.base.jfinal.directive.annotation.JfsaDirective;
import com.jfinal.kit.StrKit;
import com.jfinal.template.Env;
import com.jfinal.template.TemplateException;
import com.jfinal.template.io.Writer;
import com.jfinal.template.stat.Scope;

import java.io.IOException;

@JfsaDirective("themeOption")
public class ThemeOptionDirective extends BaseDirective {
	public void onRender(Env env, Scope scope, Writer writer) {

		String key = getPara(0, scope);
		if (StrKit.isBlank(key)) {
			throw new IllegalArgumentException("#themeOption(...) argument must not be empty");
		}

		String defaultValue = getPara(1, scope, "");

		String value = ThemeManager.me().getCurrentTheme().getOption(key);
		if (StrKit.isBlank(value)) {
			value = defaultValue;
		}

		try {
			writer.write(value);
		} catch (IOException e) {
			throw new TemplateException(e.getMessage(), location, e);
		}
	}
}
